﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="AccountFilter.cs" company="Concept Architecture">
//   (c) 2010 Muhammad Umer Farooq
// </copyright>
// <summary>
//   Defines the <seealso cref="Hisaab.Data.Filters.AccountFilter" /> type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Hisaab.Data.Filters
{
    using System;
    using System.Data.Objects;
    using System.Linq;
    using Entities;

    /// <summary>
    /// Defines the <seealso cref="Hisaab.Data.Filters.AccountFilter"/> type.
    /// </summary>
    public static class AccountFilter
    {
        /// <summary>
        /// Filters the specified Account collection.
        /// </summary>
        /// <param name="collection">The Account collection.</param>
        /// <param name="objectToMatch">The Account to match.</param>
        /// <returns>List of filtered Accounts</returns>
        public static IQueryable<Account> Filter(this IObjectSet<Account> collection, Account objectToMatch)
        {
            return objectToMatch == null ?
                collection :
                from account in collection
                where (objectToMatch.Id == 0 || account.Id == objectToMatch.Id) &&
                    (objectToMatch.Name == null || account.Name.Equals(objectToMatch.Name, StringComparison.InvariantCultureIgnoreCase)) &&
                    (objectToMatch.AccountNumber == null || account.AccountNumber.Equals(objectToMatch.AccountNumber, StringComparison.InvariantCultureIgnoreCase)) &&
                    (objectToMatch.AccountType == 0 || account.AccountType == objectToMatch.AccountType)
                select account;
        }
    }
}